package lanou.giftsay_01.main.categorymain;

import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import lanou.giftsay_01.R;
import lanou.giftsay_01.adapter.BodyLvAdapter;
import lanou.giftsay_01.bean.BodyBean;
import lanou.giftsay_01.tools.BaseActivity;
import lanou.giftsay_01.tools.NetTool;
import lanou.giftsay_01.tools.URLValues;

/**
 * Created by dllo on 16/8/23.
 */
public class GonglueBodyActivity extends BaseActivity {

    private ListView lv;
    private String id;
    private BodyLvAdapter bodyLvAdapter;
    private ImageView iv;
    private TextView tv;
    private TextView title;

    @Override
    protected int setLayout() {
        return R.layout.gongluebodyactivity;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        lv = (ListView) findViewById(R.id.body_ll);
        View view = LayoutInflater.from(this).inflate(R.layout.category_header , null);
        iv = (ImageView)view.findViewById(R.id.body_iv);
        tv = (TextView)view.findViewById(R.id.body_description);
        title = (TextView)findViewById(R.id.category_title);
        lv.addHeaderView(view);
    }

    @Override
    protected void initDate() {
        bodyLvAdapter = new BodyLvAdapter(this);
        NetTool netTool = new NetTool();
        netTool.getNetData(URLValues.LANMUJUMP + id + URLValues.LANMUJUMP2, BodyBean.class, new NetTool.NetListener<BodyBean>() {
            @Override
            public void onSuccess( BodyBean bodyBean) {
                       title.setText(bodyBean.getData().getTitle());
                       Picasso.with(getApplicationContext()).load(bodyBean.getData().getCover_image_url()).into(iv);
                        tv.setText(bodyBean.getData().getDescription());


                bodyLvAdapter.setBodyBean(bodyBean);
                lv.setAdapter(bodyLvAdapter);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
        lv.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                float scale = (float) getScrolly() / 200;
                float alpha = (255 * scale);
                if (view.getChildAt(0) != null) {
                    getScrolly();
                    if (getScrolly() <= 0) {
                       title.setTextColor(Color.argb((int) 0, 255, 255, 255));
                        title.setBackgroundColor(Color.argb((int) 0, 227, 29, 26));//AGB由相关工具获得，或者美工提供
                    } else if (getScrolly() > 0 && getScrolly() <= 200) {
                        //只是layout背景透明
                        title.setBackgroundColor(Color.argb((int) alpha, 227, 29, 26));
                    } else {
                        title.setBackgroundColor(Color.argb( 225, 227, 29, 26));
                        if (getScrolly()>200 && getScrolly()<= 400){

                            title.setTextColor(Color.argb((int) alpha, 255, 255, 255));
                        }
                        if (getScrolly() > 400 ){
                            title.setTextColor(Color.argb(255, 255, 255, 255));

                        }

                    }
                }
            }
        });

    }

    private int getScrolly() {
        View view = lv.getChildAt(0);
        if (view == null){
            return 0 ;
        }
        int firstVisiblePosition = lv.getFirstVisiblePosition();
        Log.d("GonglueBodyActivity", "firstVisiblePosition:" + firstVisiblePosition);
        int top = view.getTop();
        Log.d("GonglueBodyActivity", "top:" + top);
        return  -top + firstVisiblePosition*iv.getHeight();
    }
}
